package com.copm.ifm.servers.sys.controller.area;

import com.copm.ifm.base.basic.ServiceResponse;
import com.copm.ifm.base.basic.pojo.vo.BaseTreeVO;
import com.copm.ifm.base.service.base.BaseController;
import com.copm.ifm.servers.sys.entity.area.SysArea;
import com.copm.ifm.servers.sys.service.area.SysAreaService;
import com.copm.ifm.servers.sys.vo.SysAreaVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 区域表相关接口类
 * *
 * * @author tp
 * * @date 2021/06/08 15:18
 */
@Slf4j
@Api(tags = "区域/sys-area")
@RestController
@RequestMapping("/sys-area")
public class SysAreaController extends BaseController {

    @Autowired
    SysAreaService service;

    @ApiOperation(value = "树状图-获取项目层级树状图")
    @GetMapping("/tree")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "是否启用 [0代表未启用的，1是查启用的]", name = "enabled", defaultValue = "0"),
            @ApiImplicitParam(value = "项目id，主要条件", name = "projectId"),
            @ApiImplicitParam(value = "查询条件（按名称搜索）", name = "name")})
    public ServiceResponse<? extends BaseTreeVO> tree(@RequestParam(defaultValue = "0") int enabled,
                                                      @RequestParam Integer projectId,
                                                      @RequestParam String name) {
        return success(service.getTree(enabled, projectId, name));
    }

    @ApiOperation("添加-项目层级表")
    @PostMapping("/add/area")
    public ServiceResponse<SysArea> add(@ApiParam("添加项目层级表单对象")
                                        @RequestBody SysArea sysArea) {
        //处理父路径参数值，当父值不为空，或者删除头尾为空的时候；
        if (sysArea.getParentPath() != null && ("").equals(sysArea.getParentPath().trim())) {
            sysArea.setParentPath(null);
        }
        return success(service.add(sysArea));
    }

    @ApiOperation("点击新增一级分类/新增下级分类")
    @GetMapping("/click/add/areas")
    public ServiceResponse<SysAreaVO> clickAddEqu(@ApiParam("父级ID，为空时则增加的是一级分项，非空时将在该ID下新增分项")
                                                  @RequestParam Integer parentAreaId) {
        SysAreaVO sysAreaVO = service.clickAddOrg(parentAreaId);
        return success(sysAreaVO);
    }

}
